package com.bocai.freeman.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bocai.freeman.R;
import com.bocai.freeman.view.custom.NoScrolGridView;
import com.bumptech.glide.Glide;
import com.jakewharton.rxbinding.view.RxView;

import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observable;

/**
 * Created by win7 on 2016/5/26.
 */
public class PerfectLightAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<List<Integer>> mdatas;
    private boolean mIsLoadImage = true;
    private OnItemClickLitener mOnItemClickLitener;

    public PerfectLightAdapter(Context context, List<List<Integer>> datas) {
        this.mContext = context;
        this.mdatas = datas;
    }

    /**
     * 是否加载图片
     *
     * @param isLoadImage
     */
    public void setLoadImage(boolean isLoadImage) {
        this.mIsLoadImage = isLoadImage;
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_perfect_light, parent, false);
        return new PerfectLightHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        click(holder.itemView).subscribe(o -> {
            int pos = holder.getLayoutPosition();
            mOnItemClickLitener.onItemClick(holder.itemView, pos);
        });
        setUpImage((PerfectLightHolder) holder, mdatas.get(position), position);
    }

    @Override
    public int getItemCount() {
        return mdatas.size();
    }

    /**
     * 设置图片显示规则
     *
     * @param holder
     * @param list
     * @param position
     */
    private void setUpImage(PerfectLightHolder holder, List<Integer> list, int position) {
        holder.flImage.setVisibility(View.GONE);
        if (list != null) {
            if (list.size() == 0) {
                holder.flImage.setVisibility(View.GONE);
            } else if (list.size() == 1) {
                setSingleImage(holder, position);
                holder.gvImage.setVisibility(View.GONE);
                holder.ivImage.setVisibility(View.VISIBLE);
                holder.flImage.setVisibility(View.VISIBLE);
            } else {
                holder.ivImage.setVisibility(View.GONE);
                holder.gvImage.setVisibility(View.VISIBLE);
                holder.flImage.setVisibility(View.VISIBLE);
                setManyImage(list, holder, position);
            }
        } else {
            holder.flImage.setVisibility(View.GONE);
        }
    }

    /**
     * 设置多图
     *
     * @param list
     * @param holder
     * @param position
     */
    private void setManyImage(List<Integer> list, PerfectLightHolder holder, int position) {
        Log.e("tag",list.size() + "picList");
        PerfectLightGridAdapter perfectLightGridAdapter = new PerfectLightGridAdapter(mContext, list, mIsLoadImage);
        holder.gvImage.setAdapter(perfectLightGridAdapter);
    }

    /**
     * 设置单图
     *
     * @param holder
     */
    private void setSingleImage(PerfectLightHolder holder, int position) {
        Glide.with(mContext)
                .load(R.mipmap.img01)
                .crossFade()
                .placeholder(R.mipmap.app_network_off)
                .into(holder.ivImage);
          holder.ivImage.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Toast.makeText(mContext, "singleImage", Toast.LENGTH_SHORT).show();
              }
          });
    }

    class PerfectLightHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.icon)
        ImageView icon;
        @Bind(R.id.tv_nickname)
        TextView tvNickname;
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.ll_replay_count)
        LinearLayout llReplayCount;
        @Bind(R.id.tv_label)
        TextView tvLabel;
        @Bind(R.id.img_member_level)
        ImageView imgMemberLevel;
        @Bind(R.id.tv_text)
        TextView tvText;
        @Bind(R.id.gv_image)
        NoScrolGridView gvImage;
        @Bind(R.id.iv_image)
        ImageView ivImage;
        @Bind(R.id.fl_image)
        FrameLayout flImage;
        @Bind(R.id.cb_like)
        CheckBox cbLike;
        @Bind(R.id.cb_comment)
        TextView cbComment;

        public PerfectLightHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private <T extends Object> Observable click(View view) {
        return RxView.clicks(view)
                .throttleFirst(1000, TimeUnit.MILLISECONDS);
    }
}

